using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;

namespace CoverageRunner
{
    public class VisualStudioInstrumentation
    {
        private readonly string vsinstrLocation;

        public VisualStudioInstrumentation(string vsinstrLocation)
        {
            this.vsinstrLocation = vsinstrLocation;
        }

        public void Instrument(string outputPath, IEnumerable<string> targetsToInstrument)
        {
            foreach (string target in targetsToInstrument)
            {
                string arguments = string.Format("/COVERAGE \"{0}\" /OUTPUTPATH:\"{1}\"", target, outputPath);

                Process p = new Process();
                p.StartInfo.FileName = Path.Combine(vsinstrLocation, "vsinstr.exe");
                p.StartInfo.Arguments = arguments;
                p.Start();
                p.WaitForExit();

                if (p.ExitCode!=0) throw new ApplicationException("Unable to successfully instrument targets");
            }
            
            //TODO : Bring in dependency assemblys 
        }
    }
}